package com.lvchangqi.util;

import com.lvchangqi.pojo.Notice;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by version on 17-6-27.
 */
@Component
public class College {
    private static final String BASE_URL = "http://www.et.hbnu.edu.cn/";

    private static final String STUDIO_URL = "http://www.et.hbnu.edu.cn/new?status=xmxz4";

    public List getInstance() throws IOException {
        Connection conn = Jsoup.connect(BASE_URL);
        Connection.Response resp = conn.method(Connection.Method.GET).execute();
        Document doc = resp.parse();
        Element div = doc.getElementsByClass("notice").first();
        Elements li = div.getElementsByTag("li");
        final List<Notice> notices = new ArrayList<>();
        li.forEach((e) -> {
            Elements a = e.getElementsByTag("a");
            Elements span = e.getElementsByTag("span");
            Notice notice = new Notice();
            notice.setTime(span.get(0).text());
            notice.setTimeHref(a.get(0).attr("href"));
            notice.setTitle(a.get(1).text());
            notice.setTitleHref(a.get(1).attr("href"));
            notices.add(notice);
        });

        return notices;
    }

    public List  studio() throws IOException {
        Connection conn = Jsoup.connect(STUDIO_URL);
        Connection.Response resp = conn.method(Connection.Method.GET).execute();
        Document doc = resp.parse();
        Element div = doc.getElementsByClass("scrollleft").first();
        Elements li = div.getElementsByTag("li");

        final List<Map<String, String>> studios = new ArrayList<>();
        li.forEach((e) -> {
            Map<String, String> map = new HashMap<>();
            Element span = e.getElementsByTag("span").first();
            Element a = e.getElementsByTag("a").first();
            Element img = a.getElementsByTag("img").first();
            map.put("name", span.text());
            map.put("logo", BASE_URL + img.attr("src"));
            map.put("url", a.attr("href").split("=")[1]);
            map.put("step", String.valueOf(studios.size()));
            studios.add(map);
        });

        return studios;
    }

    public Map studioDetail(String status, Integer step) throws IOException {
        Connection conn = Jsoup.connect(BASE_URL + "new?status=" + status);
        Connection.Response resp = conn.method(Connection.Method.GET).execute();
        Document doc = resp.parse();
        Map<String, Map> map = new HashMap<>();
        Map<String, String> titleMap = new HashMap<>();
        Map<String, Object> contentMap = new HashMap<>();

        String searchCondition = "one";
        if(step%2 != 0){
            searchCondition = "two";
        }
        Elements containers = doc.getElementsByClass(searchCondition);
        Element container = containers.first();
        if(step == 4){
            container = containers.get(1);
        }

        Element title = container.getElementsByClass("one_title").first();
        Element content = container.getElementsByClass(searchCondition + "_content").first();

        String logo = title.getElementsByTag("img").first().attr("src");
        String name = title.getElementsByTag("span").first().text();
        titleMap.put("logo", BASE_URL + logo);
        titleMap.put("name", name);

        String img = content.getElementsByTag("img").first().attr("src");
        Elements p = content.getElementsByTag("p");
        List<Map> text = new ArrayList<>();
        p.forEach((e) -> {
            Map<String, String> textMap = new HashMap<>();
            textMap.put("weight", e.text().substring(0,5));
            textMap.put("text", e.text().substring(5));
            text.add(textMap);
        });
        contentMap.put("img", BASE_URL + img);
        contentMap.put("text", text);

        map.put("title", titleMap);
        map.put("content", contentMap);

        return map;
    }
}
